package gameplay;
import ia.IA;

import java.util.*;



public class Game {

  private boolean isIA = false;
  private IA ia;
  private Grid grid;
  private int gameChoice = 4;
  private Scanner sc = new Scanner(System.in);
  private static Random randomNumberGenerator;
  private ArrayList<Integer> chips;
  private long seed = System.currentTimeMillis() / 1000L;
  private int scoreTotal = 0;
  private int numberOfGame = 0;
  private boolean automaticMode = false;
  static boolean print = true;
  
  public void launch() throws InterruptedException {
    PathGroup pathGroupTest = new PathGroup();
    System.out.println("Score test : " + pathGroupTest.testGetPathListScore());
    
    // fillRandomly();
    boolean gameContinue = true;
    initRandomNumberGenerator();
    while (gameContinue && numberOfGame < 1000) {
       numberOfGame++;
    	 printMenu();
    	  // sc.nextLine();
    	  grid = new Grid("grid" + gameChoice + ".txt");
        grid.printGrid();
        grid.findPathGroups();
        // grid.setStartingPoint(new Point(3,3));
        
        initChips();
        initPlayer();

        while (!grid.isGridFilled()) {
        	playRound();
        }
       displayScore();
       
       gameContinue = continueTheGame();
    }
    System.out.println("♫ This is the end ♫ ... ");
    printStats();
    
  }

  /**
   * Prints Menu.
   */
  private void printMenu() {
    if (!print) {
      return;
    }
	System.out.println("Which game ? : ");
  	System.out.println("  1. Basic.");
  	System.out.println("  2. Loop.");
  	System.out.println("  3. Double Loop.");
  	System.out.println("  4. Surface.");
  	System.out.println("  5. Custom.");  	
  	//this.gameChoice = sc.nextInt();  
  }

  
  private void initRandomNumberGenerator() {
	  System.out.println("Votre seed est  " + seed);
      randomNumberGenerator =  new Random(seed);
  }
  
  /**
   * Initializes the array of chips.
   */
  private void initChips() {
	  chips = new ArrayList<Integer>();
	  for (int i = 1; i <= 30; i++) {
		  chips.add(i);
		  if (10 < i && i <= 19) {
		    chips.add(i);
		  }
	  }
	  chips.add(31);
  }
  
  /**
   * Plays a round.
   * @throws InterruptedException 
   */
  private void playRound() throws InterruptedException {
	  int nextChip = getRandomChip();
	  if (print) {
	    System.out.println(nextChip == 31 ? "You Picked the Joker (31) ! " : "You picked the chips : " + nextChip);
    }

	  printChips();
	  Point point;
	  int asked = 0;
	  do {
	    if (!isIA || asked > 4) {
	      System.out.println("Where do you want to place it ? ");
	      String coordinates = sc.nextLine();
	      point = Util.coordinatesToPoint(coordinates, grid);	      
	    } else {
	      point = ia.getNextPointGridOne(this, nextChip);
	    }
	    asked++;
	  } while (!grid.isValidPoint(point));
		  
	  
	  grid.setPointInGrid(point, nextChip);
	  
	  grid.printGrid();
  }
  
  
  /**
   * Gets a random chips.
   * @return {Integer}
   */
  private int getRandomChip() {
	  int index = Math.abs(randomNumberGenerator.nextInt()) % chips.size();
	  int newChips = chips.get(index);
	  chips.remove(index);
	  return newChips;
  }
  
  /**
   * 
   */
  private void displayScore() {
    int score = grid.findScore();
    scoreTotal += score;
    if (!print) {
      return;
    }
    System.out.println("Votre score est " + score);
  }
  
  private void printChips() {
    if (!print) {
      return;
    }
    System.out.print("Chips : " + chips.size() + " " + chips);
    System.out.println("");
  }
  
  private boolean continueTheGame() {
    if (automaticMode)
      return true;
    String isContinue = "";
    char letter = ' ';
    do {
      System.out.println("Do you want to start another game (Y/N) ?");
      isContinue = sc.nextLine();
      letter =  Character.toUpperCase(isContinue.charAt(0));
    } while (letter != 'Y' && letter != 'N');
    return letter == 'Y';
  }
  
  /**
   * 
   */
  private void initPlayer() {
    String checkIA = "";
    char letter = ' ';
    do {
      if (print) {
        System.out.println("is IA playing (Y/N) ?");        
      }
      if (automaticMode) {
        checkIA = "Y";
      } else {
        checkIA = sc.nextLine();
      }
      letter =  Character.toUpperCase(checkIA.charAt(0));
    } while (letter != 'Y' && letter != 'N');
    isIA = letter == 'Y';
    if (isIA) {
      ia = new IA(this);
      if (print) {
        System.out.println("IA is playing.");
      }
    }
  }

  
  private void printStats() {
    int mean =  numberOfGame == 0 ? 0 : (scoreTotal/numberOfGame);
    System.out.println("Your total score for" + numberOfGame + " games is :  ... "
        + scoreTotal + " that is : " + mean + " per game");     
  } 

  
  public Grid getGrid () {
    return grid;
  }
  
  public ArrayList<Integer> getChips() {
    return chips;
  }   

  
}
